package ac.neec.mio.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import ac.neec.data.MajorHeading;
import ac.neec.db.service.SQLManager;
import ac.neec.mio.R;
import ac.neec.mio.record.search.CallbackListener;
import ac.neec.mio.ui.adapter.SearchSelectListAdapter;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

@SuppressLint("ValidFragment")
public class SelectListDialogFragment extends DialogFragment{

	private Dialog dialog;
	private ListView listView;
	private CallbackListener listener;
	private SearchSelectListAdapter adapter;
	private List<MajorHeading> list;
	private List<String> selectMenu;

	public SelectListDialogFragment(){
	}

	public SelectListDialogFragment(CallbackListener listener){
		this.listener = listener;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		dialog = super.onCreateDialog(savedInstanceState);
		//        dialog = new Dialog(getActivity());
		dialog.setContentView(R.layout.dialog_select_list);
		//	     dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setType(STYLE_NO_FRAME);
		View view = getActivity().getLayoutInflater().inflate(
				R.layout.dialog_select_list, null);

		//		if (bundle != null)
		//			listener = (CallbackListener) bundle.getSerializable("callback");
		listView = (ListView) dialog.findViewById(R.id.select_list_view);

		createList();
		adapter = new SearchSelectListAdapter(getActivity(),
				R.layout.select_list_item, selectMenu);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
											int arg2, long arg3) {
				Log.e("", "onClickItem = "+selectMenu.get(arg2));
				Log.e("", "listener = "+listener);
				listener.select(selectMenu.get(arg2));
				dismiss();
			}
		});
		return dialog;
	}

	private void createList() {
		list = SQLManager.selectMajorHeadingAll();
		selectMenu = new ArrayList<String>();
		for (MajorHeading tmp : list) {
			selectMenu.add(tmp.getTitleJP());
		}
	}


}
